﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using starLiGHT.GUI.Widgets;
using starLiGHT.GUI;

namespace starLiGHT.GUI.Widgets
{
    public class ClickableStateStepper : Button
    {
        string[] states;
        int currentState;

        public event EventHandler StateChanged;

        public ClickableStateStepper(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
            List<string> tmp = new List<string>();
            foreach(State s in Layout.States)
                tmp.Add(s.Name);

            states = tmp.ToArray();
        }

        public int CurrentStateIndex { 
            get { return currentState; }
            set {
                if (value != currentState)
                {
                    currentState = value;
                    SetCurrentState();
                    OnStateChanged();
                }
            } 
        }

        public override void OnMouseEnter(object sender, MouseEventArgs e)
        {
            base.OnMouseEnter(sender, e);
            SetCurrentState();
        }

        public override void OnMouseLeave(object sender, EventArgs e)
        {
            base.OnMouseLeave(sender, e);
            SetCurrentState();
        }
        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            base.OnMouseUp(sender, e);
            SetCurrentState();
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(sender, e);
            SetCurrentState();
        }

        public override void OnClick(object sender, EventArgs e)
        {
            base.OnClick(sender, e);

            currentState = (currentState + 1) % states.Length;
            SetCurrentState();
            OnStateChanged();
        }
        private void SetCurrentState()
        {
            SetState(states[currentState % states.Length]);
        }

        public void OnStateChanged()
        {
            if (StateChanged != null)
            {
                StateChanged(this, new EventArgs());
            }
        }


    }
}
